package com.example.wjh.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;


import com.example.wjh.entity.Game;
import com.example.wjh.entity.Result;
import com.example.wjh.mapper.GameMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 比赛
 * 后端接口
 * @author
 * @email
 * @date 2022-07-31 07:01:29
 */
@CrossOrigin
@RestController
@RequestMapping("/game")
public class GameController {
    @Autowired
    private GameMapper GameMapper ;
    /**
     * 查询所有比赛
     * @return
     */
    @GetMapping()
    public Result getGames(){  return Result.success(GameMapper .selectList(null));}

    /**
     * 添加
     * @param
     * @return
     */
    @PostMapping()
    public Result createGame(@RequestBody Game game) {
        GameMapper.insert(game);
        return Result.success();
    }

    /**
     * 更新
     * @param id
     * @param
     * @return
     */
    @PutMapping("/{id}")
    public Result updateGame(@PathVariable("id") int id, @RequestBody Game Game) {
        Game existingGame = GameMapper.selectById(id);
        if (existingGame != null) {
            Game.setId(id);
            GameMapper.updateById(Game);
            return Result.success();
        } else {
            return Result.error("Player not found");
        }
    }


    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result delPlayer(@PathVariable("id") int id) {
        GameMapper.deleteById(id);
        return Result.success();
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @DeleteMapping("/batch")
    public Result delGameBatch(@RequestBody List<Long> ids) {
        GameMapper.deleteBatchIds(ids);
        return Result.success();
    }

    /**
     * 模糊查询
     * @param
     * @return
     */
    @GetMapping("/search")
    public Result searchGame(
            @RequestParam(required = false) String hometeamname,
            @RequestParam(required = false) String visitingteam,
            @RequestParam(required = false) String gameplace
    ) {
        return Result.success(GameMapper.searchGame(hometeamname,visitingteam,gameplace));
    }


    /**
     * 分页
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/findByPage")
    public Result getGameList(@RequestParam("pageNum") Integer pageNum,
                              @RequestParam("pageSize") Integer pageSize) {
        Page<Game> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Game> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id"); // 根据id字段降序排序
        page.addOrder(OrderItem.desc("id")); // 添加降序排序条件
        IPage ipage = GameMapper.selectPage(page,null);
        return Result.success(ipage);
    }

}

